#ifndef SM2_KEY_EXCHANGE_H
#define SM2_KEY_EXCHANGE_H

#include <stdint.h>
#include <stddef.h>
#include <gmssl/sm2.h>

// SM2密钥交换上下文 - 直接使用GmSSL的类型和API
typedef struct sm2_key_exchange_ctx_st
{
    unsigned char id_a[16]; // 发起方ID
    size_t id_a_len;
    unsigned char id_b[16]; // 响应方ID
    size_t id_b_len;
    unsigned char r_a[32]; // 随机数RA
    unsigned char r_b[32]; // 随机数RB
    unsigned char k[32];   // 共享密钥
    SM2_KEY key_pair;      // GmSSL SM2密钥对
    int has_private_key;   // 是否包含私钥
} SM2_KEY_EXCHANGE_CTX;

// 初始化密钥交换上下文
int sm2_key_exchange_init(SM2_KEY_EXCHANGE_CTX *ctx);

// 生成密钥交换参数
int sm2_generate_exchange_params(SM2_KEY_EXCHANGE_CTX *ctx);

// 导出公钥
int sm2_export_public_key(SM2_KEY_EXCHANGE_CTX *ctx, unsigned char *out, size_t *outlen);

// 协商密钥
int sm2_key_negotiate(SM2_KEY_EXCHANGE_CTX *initiator_ctx, SM2_KEY_EXCHANGE_CTX *responder_ctx,
                      const unsigned char *remote_pub_x, size_t remote_pub_x_len,
                      const unsigned char *remote_pub_y, size_t remote_pub_y_len,
                      unsigned char *k, size_t klen);

// 清理密钥交换上下文
void sm2_key_exchange_cleanup(SM2_KEY_EXCHANGE_CTX *ctx);

#endif /* SM2_KEY_EXCHANGE_H */
